#include "head.h"

void set_user(u_list *list, u_list *list_regual, u_list *list_silver, u_list *list_gold, u_list *list_platinum)
{
    // 安全判断
    if (NULL == list_regual || NULL == list_silver || NULL == list_gold || NULL == list_platinum)
    {
        ERRLOG("链表的动态空间为空");
        return;
    }
    if (NULL == list_regual->next || NULL == list_silver->next || NULL == list_gold->next || NULL == list_platinum->next)
    {
        ERRLOG("传入的表内为空");
        return;
    }
    u_node *ptr = list->next;
    char name[128];
    int num;
    printf("请输入您要修改的用户账号:");
    scanf("%s", name);
    ptr = find_node_name(list, name);
    printf("%s%d\n", ptr->userm.name, ptr->userm.grade);
    printf("1.普通用户  2.银卡用户  3.金卡用户  4.白金用户\n");
    printf("请输入对应的数字进行修改:");
    scanf("%d", &num);
    switch (num)
    {
    case 1:
        if (ptr->userm.grade == 2)
        {
            delete_node_name(list_gold, name);
            F_u_print(list_gold, GOLD_FILE);
        }
        else if (ptr->userm.grade == 3)
        {
            delete_node_name(list_platinum, name);
            F_u_print(list_platinum, PLATINUM_FILE);
        }
        else if (ptr->userm.grade == 4)
        {
            delete_node_name(list_regual, name);
            F_u_print(list_regual, REGULAR_FILE);
        }
        else if (ptr->userm.grade == 5)
        {
            delete_node_name(list_silver, name);
            F_u_print(list_silver, SILVER_FILE);
        }
        ptr->userm.grade = 4;
        ptr->userm.rate = 1.0;
        insert_node(list_regual, ptr->userm, 1);
        F_u_print(list_regual, REGULAR_FILE);
        printf("成功将用户%s修改为普通用户", name);

        break;
    case 2:
        if (ptr->userm.grade == 2)
        {
            delete_node_name(list_gold, name);
            F_u_print(list_gold, GOLD_FILE);
        }
        else if (ptr->userm.grade == 3)
        {
            delete_node_name(list_platinum, name);
            F_u_print(list_platinum, PLATINUM_FILE);
        }
        else if (ptr->userm.grade == 4)
        {
            delete_node_name(list_regual, name);
            F_u_print(list_regual, REGULAR_FILE);
        }
        else if (ptr->userm.grade == 5)
        {
            delete_node_name(list_silver, name);
            F_u_print(list_silver, SILVER_FILE);
        }

        ptr->userm.grade = 5;
        ptr->userm.rate = 0.9;
        insert_node(list_silver, ptr->userm, 1);
        F_u_print(list_silver, SILVER_FILE);
        printf("成功将用户%s修改为银卡用户", name);
        break;
    case 3:
        if (ptr->userm.grade == 2)
        {
            delete_node_name(list_gold, name);
            F_u_print(list_gold, GOLD_FILE);
        }
        else if (ptr->userm.grade == 3)
        {
            delete_node_name(list_platinum, name);
            F_u_print(list_platinum, PLATINUM_FILE);
        }
        else if (ptr->userm.grade == 4)
        {
            delete_node_name(list_regual, name);
            F_u_print(list_gold, REGULAR_FILE);
        }
        else if (ptr->userm.grade == 5)
        {
            delete_node_name(list_silver, name);
            F_u_print(list_gold, SILVER_FILE);
        }

        ptr->userm.grade = 2;
        ptr->userm.rate = 0.8;
        insert_node(list_gold, ptr->userm, 1);
        F_u_print(list_gold, GOLD_FILE);
        printf("成功将用户%s修改为金卡用户", name);
        break;
    case 4:
        if (ptr->userm.grade == 2)
        {
            delete_node_name(list_gold, name);
            F_u_print(list_gold, GOLD_FILE);
        }
        else if (ptr->userm.grade == 3)
        {
            delete_node_name(list_platinum, name);
            F_u_print(list_platinum, PLATINUM_FILE);
        }
        else if (ptr->userm.grade == 4)
        {
            delete_node_name(list_regual, name);
            F_u_print(list_regual, REGULAR_FILE);
        }
        else if (ptr->userm.grade == 5)
        {
            delete_node_name(list_silver, name);
            print_link_list(list_silver);
            F_u_print(list_silver, SILVER_FILE);
        }

        ptr->userm.grade = 3;
        ptr->userm.rate = 0.7;
        insert_node(list_platinum, ptr->userm, 1);
        F_u_print(list_platinum, PLATINUM_FILE);
        printf("成功将用户%s修改为白金用户", name);
        break;
    default:
        break;
    }
}

/*增加超级用户*/
void add_root(u_list *list)
{
    if (NULL == list)
    {
        ERRLOG("链表的动态空间为空");
        return;
    }
    if (NULL == list->next)
    {
        ERRLOG("传入的表内为空");
        return;
    }

    user_t data;
    printf("请输入用户名:");
    scanf("%s", data.name); // 航班班次
    printf("请输入密码:");
    scanf("%s", data.passwd); // 飞机机型
    printf("请输入身份证信息:");
    scanf("%s", data.IDcard);
    getchar();
    data.value = 50000;
    data.rate = 0.0;
    data.grade = 1;
    my_memset(data.ticket);
    //strcpy(data.ticket->whetherfly,"n");
    insert_node(list, data, 1);
    F_u_print(list, ADMIN_FILE);
}

// 买票
void buy_ticket(f_list *list_flight, user_t data, u_list *list_user)
{
    if (NULL == list_flight || NULL == list_user)
    {
        ERRLOG("链表的动态空间为空");
        return;
    }
    if (NULL == list_flight || NULL == list_user)
    {
        ERRLOG("传入的表内为空");
        return;
    }
    int num;

    print_forward_double_link_list(list_flight);
    printf("输入对应航班的班次来进行购买");
    scanf("%d", &num);
    f_node *ptr = find_double_node_pos(list_flight, num);
    char buf[128];
    strcpy(buf, opi(data));
    u_node *ptr_1 = list_user->next;
    ptr_1->userm.ticket[0] = ptr->data;
    ptr_1->userm.value = ptr_1->userm.value - (ptr->data.value * ptr_1->userm.rate);
    printf("应付金额为%f :" , ptr->data.value * ptr_1->userm.rate);
    F_u_print(list_user, buf);
    printf("购票成功\n");
}

// 退票
void refund(f_list *list_flight, user_t data, u_list *list_user)
{
    if (NULL == list_flight || NULL == list_user)
    {
        ERRLOG("链表的动态空间为空");
        return;
    }
    if (NULL == list_flight || NULL == list_user)
    {
        ERRLOG("传入的表内为空");
        return;
    }

    char buf[128];
    strcpy(buf, opi(data));
    u_node *ptr_1 = list_user->next;
    f_node *ptr  = list_flight->next;
    my_memset(ptr_1->userm.ticket);
    ptr_1->userm.value = ptr_1->userm.value + (ptr->data.value * ptr_1->userm.rate);
    F_u_print(list_user, buf);
    printf("退票成功\n");
    printf("退还金额为%f\n" , ptr->data.value * ptr_1->userm.rate);
}

// 查看订单
void check(f_list *list_flight, user_t data)
{
    if (data.ticket[0].id != -1)
    {
        f_node *ptr = find_double_node_pos(list_flight, data.ticket[0].id);
        printf("班次\t机型\t\t起点站\t  终点站    起飞时间\t到达时间\t飞行时间\t票价\t载客量\t剩余座位 是否飞\n");
        printf("%-8d", ptr->data.id);                                              // 航班班次
        printf("%-16s", ptr->data.flighttype);                                     // 飞机机型
        printf("%-12s", ptr->data.startcity);                                      // 航班起点站
        printf("%-12s", ptr->data.arrivecity);                                     // 航班终点站
        printf("%02d:%02d\t", ptr->data.starttime[0], ptr->data.starttime[1]);     // 起飞时间
        printf("%02d:%02d\t\t", ptr->data.arrivetime[0], ptr->data.arrivetime[1]); // 到达时间
        printf("%-10s\t", ptr->data.flytime);                                      // 飞行时间
        printf("%-8d", ptr->data.value);                                           // 票价
        printf("%-8d", ptr->data.maxNUM);                                          // 额定载客量
        printf("%-10d", ptr->data.leftbuyersa);                                    // 剩余座位数量
        printf("%c\n", ptr->data.whetherfly);
    }
    else
    {
        printf("您没有待出行的订单\n");
    }


}

// 充钱
void Recharge_balance(user_t data, u_list *list)
{
    if (NULL == list)
    {
        ERRLOG("链表的动态空间为空");
        return;
    }
    if (NULL == list->next)
    {
        ERRLOG("传入的表内为空");
        return;
    }
    char buf[128];
    strcpy(buf, opi(data));
    int money;
    printf("请输入你要充值的金额");
    scanf("%d", &money);
    data.value = data.value + money;
    u_node *ptr = list->next;
    ptr->userm.value = data.value;
    F_u_print(list, buf);
    printf("充值成功,当前余额为%d\n", ptr->userm.value);
}

// 用户修改密码
void c_passwd(user_t data, u_list *list)
{
    char buf[128];
    char file[128];
    strcpy(file, opi(data));
    printf("请输入新的密码:");
    scanf("%s", buf);
    if (!strcmp(data.passwd, buf))
    {
        printf("你个笨蛋 新密码 不能和旧密码相同\n");
        return;
    }

    u_node *ptr = list->next;
    strcpy(ptr->userm.passwd, buf);
    printf("密码修改成功 新密码为 %s\n", buf);
    F_u_print(list, file);
}

// 判断用户的会员 返回该会员的文件
char *opi(user_t data)
{
    if (data.grade == 2)
    {
        return GOLD_FILE;
    }
    else if (data.grade == 3)
    {
        return PLATINUM_FILE;
    }
    else if (data.grade == 4)
    {
        return REGULAR_FILE;
    }
    else if (data.grade == 5)
    {
        return SILVER_FILE;
    }

    return NULL;
}
